package com.cpi.basilos.unit.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.cpi.basilos.corntype.view.CornTypeView;
import com.cpi.basilos.unit.dto.UnitByOriginDTO;
import com.cpi.basilos.unit.dto.UnitDTO;
import com.cpi.basilos.unit.exception.UnitException;
import com.cpi.basilos.unit.view.UnitByOriginView;
import com.cpi.basilos.unit.view.UnitView;
import com.cs.common.exception.CSException;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.TimeUtils;

public class UnitConverter {
	private static org.apache.log4j.Logger logger = Logger.getLogger(UnitConverter.class);
	public UnitByOriginView convertUnitByOriginDTOToView(UnitByOriginDTO unitByOriginDTO) throws UnitException {
		UnitByOriginView unitByOriginView = null;
		try{
			unitByOriginView = new UnitByOriginView();
			unitByOriginView.setRoute(unitByOriginDTO.getRoute());
			unitByOriginView.setCornType(unitByOriginDTO.getCornType());
			unitByOriginView.setTransportType(unitByOriginDTO.getImage());
			unitByOriginView.setHoppers(unitByOriginDTO.getHoppers());
			unitByOriginView.setAmount(NumberUtil.convertQuantity(unitByOriginDTO.getAmount()));
			unitByOriginView.setDate(TimeUtils.convertJavaDateToString(unitByOriginDTO.getDate().getTime()));
		}catch(Exception exception){
			UnitException unitException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			unitException = new UnitException(exception,CSException.LAYER_CONVERTER, CSException.ACTION_SELECT);
			throw unitException;
		}
		return unitByOriginView;
	}
	public UnitView convertUnitDTOToView(UnitDTO unitDTO) throws UnitException {
		UnitView unitView = null;
		CornTypeView cornTypeView = null;
		try{
			unitView = new UnitView();
			cornTypeView = new CornTypeView();
			unitView.setId(unitDTO.getId());
			unitView.setRoute(unitDTO.getRoute());
			unitView.setAmount(NumberUtil.convertQuantity(unitDTO.getAmount()));
			unitView.setDate(TimeUtils.convertJavaDateToString(unitDTO.getDate().getTime()));
			cornTypeView.setCornTypeId(unitDTO.getCornTypeId());
			unitView.setCornTypeView(cornTypeView);
			unitView.setInitial(unitDTO.getInitial());
			unitView.setNumber(unitDTO.getNumber());
			unitView.setSeals(unitDTO.getSeals());
			unitView.setOriginWetness(NumberUtil.convertQuantity(unitDTO.getOriginWetness()));
			unitView.setGuide(unitDTO.getGuide());
		}catch(Exception exception){
			UnitException unitException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			unitException = new UnitException(exception,CSException.LAYER_CONVERTER, CSException.ACTION_SELECT);
			throw unitException;
		}
		return unitView;
	}
	
	public List convertUnitDTOsToViews(List unitDTOs) throws UnitException {
		List unitViews = null;
		UnitView unitView = null;
		UnitDTO unitDTO = null;
		Iterator iteratorDTOs = null;
		try{
			iteratorDTOs = unitDTOs.iterator();
			unitViews = new ArrayList(unitDTOs.size());
			while(iteratorDTOs.hasNext()){
				unitDTO = (UnitDTO)iteratorDTOs.next();
				unitView = convertUnitDTOToView(unitDTO);
				unitViews.add(unitView);
			}
		}catch(UnitException unitException){
			throw unitException;
		}catch(Exception exception){
			UnitException unitException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			unitException = new UnitException(exception,CSException.LAYER_CONVERTER, CSException.ACTION_SELECT);
			throw unitException;
		}
		return unitViews;
	}
}